package xyz.xtt.equity.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import xyz.xtt.common.starter.cache.CacheManager;
import xyz.xtt.equity.constants.AssetCache;
import xyz.xtt.equity.entity.TblAssetEntitlement;
import xyz.xtt.equity.entity.TblAssetEntitlementDeliver;
import xyz.xtt.equity.mapper.TblAssetEntitlementMapper;
import xyz.xtt.equity.service.ITblAssetEntitlementDeliverService;
import xyz.xtt.equity.service.ITblAssetEntitlementService;

/**
 * <p>
 * 资产权益表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-30
 */
@Service
public class TblAssetEntitlementServiceImpl extends ServiceImpl<TblAssetEntitlementMapper, TblAssetEntitlement> implements ITblAssetEntitlementService {
	@Autowired
	private CacheManager cacheManager;
	@Autowired
	private ITblAssetEntitlementDeliverService iTblAssetEntitlementDeliverService;
	@Override
	public TblAssetEntitlement info(Long id) {
		return getById(id);
	}

	@Override
	public List<TblAssetEntitlement> getAssetEntitlement(Long assetId) {
		return cacheManager
				.cacheList(() -> new LambdaQueryChainWrapper<>(baseMapper)
						.eq(TblAssetEntitlement::getAssetId, assetId)
						.list(), AssetCache.TBL_ASSET_ENTITLEMENT, String.valueOf(assetId));
	}

	/**
	 * 根据权益ID 判断是否拥有权益
	 * 
	 * @param uid
	 * @param id
	 * @return
	 */
	@Override
	public boolean deliverCheck(String uid, Long id) {
		TblAssetEntitlement info = info(id);
		if (Objects.isNull(info)) {
			return false;
		}
		// 根据权益ID和用户ID 获取权益兑换信息
		TblAssetEntitlementDeliver deliver = iTblAssetEntitlementDeliverService.info(info.getId(), uid);
		return !Objects.isNull(deliver);
	}
}
